<template>
  <!-- 邮箱模版 -->
  <el-dialog :visible.sync="isShow" v-if="isShow" title="邮箱模版" width="50%" :before-close="handleClose" top="100px" :close-on-click-modal="false">
    <div class="border-box">
      <el-form ref="mailBoxForm" :model="formParams" :rules="ruleForm" label-position="right" label-width="0">
        <div class="flex item-box" v-for="(item,index) in itemData" :key="index" style="border-bottom:none">
          <div class="title width25">
            {{ item.label }}<span :class="item.pass?'mustPass':'noMustPass'">*</span>
          </div>
          <div class="content width80">
            <el-form-item class="item-clear" v-show="item.key=='template_id'" label="" prop="template_id" >
              <el-input-number :min="1" :max="999999999" :precision="0" v-model="formParams.template_id" controls-position="right" clearable placeholder="请输入模版ID"/>
            </el-form-item>
            <el-form-item class="item-clear" v-show="item.key=='template_title_id'" label="" prop="template_title_id" >
              <el-select v-model="formParams.template_title_id" placeholder="请选择标题" filterable>
                <el-option :label="second.c_base_zh_cn" :value="second.a_base_message_id" v-for="(second, two) in multilingualData" :key="two"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item class="item-clear" v-show="item.key=='template_context_id'" label="" prop="template_context_id" >
              <el-select v-model="formParams.template_context_id" placeholder="请选择内容" filterable>
                <el-option :label="second.c_base_zh_cn" :value="second.a_base_message_id" v-for="(second, two) in multilingualData" :key="two"></el-option>
              </el-select>
            </el-form-item>
          </div>
        </div>
      </el-form>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button size="medium" @click="handleClose">取 消</el-button>
      <el-button size="medium" type="primary" @click="handleConfirm('mailBoxForm')">确 认</el-button>
    </span>
  </el-dialog>
</template>

<script>
import {
  addEmailTemplate,
  editEmailTemplate
} from "@/api/config";

export default {
  components: {},
  props: {
    show: {
      type: Boolean,
      default: false
    },
    data: {
      type: Object,
      default: function () {
        return {}
      }
    },
    multilingualData: {
      type: Array,
      default: function () {
        return []
      }
    },
  },
  data () {
    return {
      isShow: false,
      itemData:[
        { label: '模版ID', key: 'template_id' ,pass:true},
        { label: '模版标题', key: 'template_title_id' ,pass:true},
        { label: '模版内容', key: 'template_context_id' ,pass:true},
      ],
      formParams: {
        template_title_id: '', //标题 多语言id
        template_context_id: '', //内容 多语言id
        template_id: '', //模版ID
      },
      ruleForm: {
        template_id: [{ required: true, trigger: 'blur', message: '请输入模版ID', }],
        template_title_id: [{ required: true, trigger: 'blur', message: '请选择模版标题', }],
        template_context_id: [{ required: true, trigger: 'blur', message: '请选择内容标题', }],
      },
    }
  },
  computed: {},
  watch: {
    show: function () {
      let that = this;
      that.isShow = that.show
      if (that.data) {
        that.formParams = JSON.parse(JSON.stringify(that.data))
      } else {
        that.formParams = {
          template_id: '',
          template_title_id: '',
          template_context_id: '',
        }
      }
    },
  },
  mounted () { },
  methods: {
    // 确认
    handleConfirm (formvalue) {
      let that = this,
      params = JSON.parse(JSON.stringify(that.formParams));
      that.$refs[formvalue].validate((valid) => {
        if (valid) {
          if (!params.id) {
            addEmailTemplate(params).then(function (res) {
              that.$message.success(res.msg)
              that.$emit('onHideDetails', true)
            }).catch((error) => {
              that.$message.error(error.msg)
            })
          } else {
            editEmailTemplate(params).then(function (res) {
              that.$message.success(res.msg)
              that.$emit('onHideDetails', true)
            }).catch((error) => {
              that.$message.error(error.msg)
            })
          }
        } else {
          return false;
        }
      });
    },
    // 取消
    handleClose () {
      this.$emit('onHideDetails', false)
    },
  }
}
</script>

<style scoped lang="scss">
</style>
